/**
 * API 响应处理工具
 */

import type { AxiosResponse } from 'axios'
import type { ApiResponse, PaginatedApiResponse } from '@/types/blog.types'

/**
 * 处理 API 响应，提取实际数据
 * 由于响应拦截器已经处理了数据，这里进行类型断言
 */
export function handleApiResponse<T>(response: AxiosResponse<ApiResponse<T>>): ApiResponse<T> {
  return response as unknown as ApiResponse<T>
}

/**
 * 处理分页 API 响应
 */
export function handlePaginatedApiResponse<T>(
  response: AxiosResponse<PaginatedApiResponse<T>>,
): PaginatedApiResponse<T> {
  return response as unknown as PaginatedApiResponse<T>
}

/**
 * 检查 API 响应是否成功
 */
export function isApiSuccess<T>(
  response: ApiResponse<T>,
): response is ApiResponse<T> & { code: 200 } {
  return response.code === 200
}

/**
 * 获取 API 响应的错误信息
 */
export function getApiErrorMessage<T>(response: ApiResponse<T>): string {
  return response.message || '未知错误'
}
